package cate.game.activity.Foodgod;

import cate.common.table.activity.foodgod.row.FoodGodBaseRow;
import cate.common.table.activity.foodgod.row.FoodGodResetRow;
import cate.common.table.activity.foodgod.row.FoodGodRewardRow;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.Foodgod.msg.FoodGodDataResp;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("食神降临")
public class FoodGodActivity extends RoleActivitySingle {
    @NoteField("累计消耗元宝数")
    public long cost;

    @NoteField("已经重置的次数")
    public int resetTimes;

    @NoteField("充值金额")
    public int pay;

    @NoteField(value = "领取记录 <tid, 是否领取>", detail = "领取之后才有记录")
    public Map<Integer, Boolean> gotMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
        role.getActivity().registerRecharge(this, this::onPay);
    }

    private void onPay(long payCents) {
        if (!enable()) {
            return;
        }
        pay += payCents / 100;
        initSend();
    }

    /**
     * 消费钻石记录
     *
     * @param consumeNum 消费数量
     */
    public void onConsumeM1(long consumeNum) {
        cost += consumeNum;
        initSend();
    }

    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {
        checkUnGot();
        cost = 0;
        resetTimes = 0;
        pay = 0;
        gotMap.clear();
    }

    @Override
    public void initSend() {
        role.sendNow(new FoodGodDataResp(this));
    }

    @Override
    public void excelCheck() {

    }

    private FoodGodBaseRow baseRow() {
        return role.getGame().table.foodGod.base.get(configTid());
    }

    @JsonIgnore
    public GameResult<Void> getReward(int tid) {
        GameResult<Void> r = new GameResult<>();
        FoodGodBaseRow baseRow = baseRow();
        if (baseRow == null) {
            return r.fail("活动未开启");
        }
        FoodGodResetRow resetRow = role.getGame().table.foodGod.reset.findRow(baseRow.resetGroup, resetTimes);
        if (resetRow == null) {
            return r.fail("奖励不存在");
        }
        FoodGodRewardRow rewardRow = role.getGame().table.foodGod.reward.get(tid);
        if (rewardRow == null || rewardRow.rewardGroup != resetRow.rewardGroup) {
            return r.fail("错误的奖励");
        }
        if (gotMap.containsKey(tid)) {
            return r.fail("奖励已领取");
        }
        if (cost < rewardRow.cost) {
            return r.fail("未达到领取条件");
        }
        gotMap.put(tid, true);
        new MixResAdder()
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .setRes(new MixRes(rewardRow.rewardStr))
                .exe(role);
        initSend();
        return r;
    }

    public GameResult<Void> reset() {
        GameResult<Void> r = new GameResult<>();
        FoodGodBaseRow baseRow = baseRow();
        if (baseRow == null) {
            return r.fail("活动未开启");
        }
        if (pay / baseRow.resetPer <= resetTimes) {
            return r.fail("没有重置次数噢");
        }
        FoodGodResetRow nextResetRow = role.getGame().table.foodGod.reset.findRow(baseRow.resetGroup, resetTimes + 1);
        if (nextResetRow == null) {
            return r.fail("只能重置" + resetTimes+ "次噢");
        }
        FoodGodResetRow nowResetRow = role.getGame().table.foodGod.reset.findRow(baseRow.resetGroup, resetTimes);
        if (nowResetRow == null) {
            return r.fail("只能重置" + resetTimes+ "次噢");
        }
        FoodGodRewardRow maxRewardRow = role.getGame().table.foodGod.reward.getMaxRow(nowResetRow.rewardGroup);
        if (maxRewardRow == null) {
            return r.fail("奖励数据异常拉");
        }
        if (cost < maxRewardRow.cost) {
            return r.fail("消耗钻石达到" + maxRewardRow.cost + "才可重置");
        }
        checkUnGot();
        resetTimes += 1;
        if (!gotMap.isEmpty()) {
            int costMax = 0;
            for (Integer tid : gotMap.keySet()) {
                FoodGodRewardRow rewardRow = role.getGame().table.foodGod.reward.get(tid);
                if (rewardRow != null && rewardRow.cost > costMax) {
                    costMax = rewardRow.cost;
                }
            }
            if (costMax > 0) {
                cost -= costMax;
            }
            gotMap.clear();
        }
        initSend();
        return r;
    }

    private void checkUnGot() {
        FoodGodBaseRow baseRow = baseRow();
        if (baseRow != null && baseRow.mailId > 0) {
            FoodGodResetRow resetRow = role.getGame().table.foodGod.reset.findRow(baseRow.resetGroup, resetTimes);
            if (resetRow != null) {
                List<FoodGodRewardRow> rows = role.getGame().table.foodGod.reward.getRows(resetRow.rewardGroup);
                if (CollectionUtils.isNotEmpty(rows)) {
                    MixRes reward = new MixRes();
                    for (FoodGodRewardRow row : rows) {
                        if (cost >= row.cost && !gotMap.containsKey(row.id)) {
                            reward.addList(new MixRes(row.rewardStr));
                            gotMap.put(row.id, true);
                        }
                    }
                    if (!reward.isEmpty()) {
                        Mail mail = new MailBuilder()
                                .setOperation(GDOperation.FOOD_GOD_AUTO_REWARD)
                                .setOperationSub(resetTimes)
                                .setContentId(baseRow.mailId)
                                .setReward(reward)
                                .build();
                        role.getMail().add(mail);
                    }
                }
            }
        }
    }
}
